import wx
import hashlib, uuid, requests


def getdisk():
	#获取硬盘号
	a = uuid.uuid1()
	a = str(a).split('-')[-1]
	b = a.encode(encoding='utf-8')
	m = hashlib.md5()
	m.update(b)
	str_md5 = m.hexdigest()
	return str_md5


def readpassword():
	#验证是否注册
	while 1:
		try:
			url = 'http://129.28.135.123:8002?key=' + getdisk()
			data = requests.get(url)
			data = data.text
			if data == '1':
				return True
			else:
				return False
		except:
			print('error code')
			exit()
				
				
class SubclassDialog(wx.Dialog):
	#输入参数后开始计算前，确认是否输入无误。
	def __init__(self, l1, cate, l3): #再初始化该类特有的属性
		wx.Dialog.__init__(self, None, id=-1, title='提示') #先初始化父类的属性
		self.l1 = l1
		self.cate = cate
		self.l3 = l3
		self.box = wx.BoxSizer(wx.VERTICAL)
		self.boxbutton = wx.BoxSizer(wx.HORIZONTAL)
		self.putonbutton()
		self.okButton = wx.Button(self, wx.ID_OK, "确定")
		self.okButton.SetDefault()
		self.cancelButton = wx.Button(self, wx.ID_CANCEL, "取消")
		self.boxbutton.Add(self.okButton,1, wx.EXPAND | wx.ALL, 10)
		self.boxbutton.Add(self.cancelButton,1, wx.EXPAND | wx.ALL, 10)
		self.box.Add(self.boxbutton, 0, wx.EXPAND)
		self.SetSizerAndFit(self.box)
		
	def putinbox(self, s, index):
		#往box里面加两列文字
		self.row_dialog += 1
		lcolumns = ['您输入的经纬度:', '您选择的行政区:', '您输入的关键词:',
		'key文件:', '您选择的POI类别:', '输出格式:', '并发线程数:']
		text1 = wx.StaticText(self, -1, lcolumns[index])
		text2 = wx.StaticText(self, -1, s)
		self.mybox.Add(text1, pos=(self.row_dialog,0),flag=wx.EXPAND)
		self.mybox.Add(text2, pos=(self.row_dialog,1),flag=wx.EXPAND)
		
	def putonbutton(self):
		#往提示框内加东西
		lloc, province, xian, shi, keywords = self.l1
		mytype, key = self.l3
		cate = self.cate
		self.mybox = wx.GridBagSizer(hgap=8, vgap=3)
		self.row_dialog = -1
		if lloc != 'null':
			s = ',  '.join(lloc)
			self.putinbox(s, 0)
		if province != 'null' or xian != 'null' or shi != 'null':
			l = [x for x in [province, xian, shi] if x != 'null']
			s = ',  '.join(l)
			self.putinbox(s, 1)
		if keywords != 'null':
			self.putinbox(keywords, 2)
		if key != 'null':
			self.putinbox('已导入', 3)
		if len(cate) > 2:
			s = '%s, %s等%s类' % (cate[0], cate[1], len(cate))
			self.putinbox(s, 4)
		else:
			s = ', '.join(cate)
			self.putinbox(s, 4)
		self.putinbox(mytype, 5)
		self.box.Add(self.mybox, 0, wx.ALL, 10)
		
class Register(wx.Dialog):

	def __init__(self, window): #再初始化该类特有的属性
		wx.Dialog.__init__(self, None, id=-1, title='POI-Player V1.0 注册') #先初始化父类的属性
		self.window = window
		self.box = wx.BoxSizer(wx.VERTICAL)
		self.buildbox()
		self.SetSizerAndFit(self.box)
		
	def buildbox(self):
		boxbutton = wx.BoxSizer(wx.HORIZONTAL)
		text = wx.StaticText(self, -1, '请求码')
		text2 = wx.TextCtrl(self, wx.NewId(), getdisk())
		text3 = wx.StaticText(self, -1, '请输入激活码')
		self.inputcode = wx.TextCtrl(self, style=wx.TE_MULTILINE)
		
		okButton = wx.Button(self, wx.ID_OK, "注册")
		okButton.SetDefault()
		cancelButton = wx.Button(self, wx.ID_CANCEL, "取消")
		boxbutton.Add(okButton,1, wx.EXPAND | wx.ALL, 10)
		boxbutton.Add(cancelButton,1, wx.EXPAND | wx.ALL, 10)
		self.box.Add(text, 0,wx.LEFT, 10)
		self.box.Add(text2, 1, wx.EXPAND| wx.ALL, 10)
		self.box.Add(text3, 0,wx.LEFT, 10)
		self.box.Add(self.inputcode, 1, wx.EXPAND| wx.ALL, 10)
		self.box.Add(boxbutton, 0, wx.EXPAND)
		self.Bind(wx.EVT_BUTTON, self.checkmac, okButton)
	
				
	def checkmac(self,event):
		#查看输入的激活码是否有效
		if readpassword():
			#注册成功
			self.window.output.AppendText('注册成功\n')
			self.Destroy()
		else:
			wx.LogError("激活码无效")
			
		
if __name__=='__main__':
	a = readpassword()
	print(a)
		
		
		